<?php

namespace app\common\model\marketing;

use addons\xyb\XypHelper;
use app\common\model\publics\Huati;
use think\Model;


class AssociationJobs extends Model
{

    // 表名
    protected $name = 'association_jobs';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    public static $grant_status_list = [
        1 => '进行中',
        2 => '已结束',
        3 => '已下架'
    ];
    public static $activity_method_list = [
        1 => '结束后发放',
        2 => '按天发放',
    ];
    protected $append = [
        'association_info',
        'status_text',
        'grant_status_text',
        'activity_method_text',
        'job_date_text',
        'create_date'
    ];

    public function getAssociationInfoAttr($value, $data){
        if(isset($data['association_id']) && $data['association_id']){
            return Association::where('id', $data['association_id'])->value('name');
        } else {
            return '';
        }
        //return Association::where('id', $data['association_id'])->value('name');
    }

    public function getGrantStatusTextAttr($value, $data){
        if(isset($data['grant_status']) && $data['grant_status']) {
            $value = $value ? $value : (isset($data['grant_status']) ? $data['grant_status'] : '');
            return isset(self::$grant_status_list[$value]) ? self::$grant_status_list[$value] : '';
        }else{
            return '';
        }
    }

    public function getStatusTextAttr($value, $data){
        if(isset($data['grant_status']) && $data['grant_status']) {
            $value = $value ? $value : (isset($data['grant_status']) ? $data['grant_status'] : '');
            return isset(self::$grant_status_list[$value]) ? self::$grant_status_list[$value] : '';
        }else {
            return '';
        }
    }
    public function getActivityMethodTextAttr($value, $data){
        if(isset($data['activity_method']) && $data['activity_method']) {
            $value = $value ? $value : (isset($data['activity_method']) ? $data['activity_method'] : '');
            return isset(self::$activity_method_list[$value]) ? self::$activity_method_list[$value] : '';
        }else {
            return '';
        }
    }
    public function getJobDateTextAttr($value, $data){
        if(isset($data['start_time']) && $data['start_time']) {
            return date('Y-m-d', $data['start_time']) . ' ~ ' . date('Y-m-d', $data['end_time']);
        }else{
            return '';
        }
    }

    public function getCreateDateAttr($value, $data){
        if(isset($data['createtime']) && $data['createtime']) {
            return date('Y-m-d H:i', $data['createtime']);
        }else{
            return '';
        }
    }

    public static function getAdminLists()
    {
        $association_id = request()->param('association_id/a');
        $keyword = request()->param('keyword', '');
        $deadline = request()->param('deadline/a');
        $grant_status = request()->param('grant_status/a');
        $field = '';
        $query = self::field($field);
        if ($association_id = XypHelper::stringToArray($association_id)) {
            $query->whereIn('association_id', $association_id);
        }
        if ($keyword) {
            $query->where(function ($query) use ($keyword) {
                $query->where('id', 'like', "%{$keyword}%")
                    ->whereOr('name', 'like', "%{$keyword}%");
            });
        }
        if ($deadline_arr = XypHelper::stringToArray($deadline)) {
            $query->where('start_time','>',  XypHelper::getTimestamp($deadline_arr[0]))
                ->where('end_time','<',  XypHelper::getTimestamp($deadline_arr[1]));
        }
        if ($grant_status = XypHelper::stringToArray($grant_status)) {
            $query->whereIn('grant_status', $grant_status);
        }
        return $query->order('id desc')->paginate(10);
    }

    public static function getAssociationJobsItems($association_id, $user_id)
    {
        $field = '';
        $jobs_lists = self::field($field);
        $jobs_lists = $jobs_lists->where('association_id', $association_id)->select();
        if(!$jobs_lists){
            return [];
        }
        $jobs_list_maps = [];
        foreach ($jobs_lists as $key => $val){
            $jobs_list_maps[$key] = [];
            $jobs_list_maps[$key]['id'] = $val['id'];
            $jobs_list_maps[$key]['name'] = $val['name'];
            $jobs_list_maps[$key]['start_date'] = date('m.d', $val['start_time']);
            $jobs_list_maps[$key]['end_date'] = date('m.d', $val['end_time']);
            $jobs_list_maps[$key]['status'] = $val['grant_status'];
            $jobs_list_maps[$key]['reward_point'] = intval($val['reward_point']);
            $jobs_list_maps[$key]['activity_method'] = intval($val['activity_method']);
            $jobs_list_maps[$key]['involved_days'] = intval($val['involved_days']);
            $jobs_list_maps[$key]['execute_days'] = intval($val['execute_days']);
            $jobs_list_maps[$key]['enroll_info'] = AssociationUsers::getUsersJobsEnrollStatus($user_id, $association_id, $val['id']);
            if(!isset($jobs_list_maps[$key]['enroll_info']['deadline_date']) || !$jobs_list_maps[$key]['enroll_info']['deadline_date']){
                $jobs_list_maps[$key]['enroll_info']['deadline_date'] = date('Y-m-d', $val['end_time']);
            }
            if(!isset($jobs_list_maps[$key]['enroll_info']['surplus_days']) || !$jobs_list_maps[$key]['enroll_info']['surplus_days']){
                $jobs_list_maps[$key]['enroll_info']['surplus_days'] =$val['execute_days'];
            }
            if ($val['activity_method'] == 2) {
                $jobs_list_maps[$key]['reward_point'] = (int)self::getEveryDayAssociationRewardPoint($val['participation_rules']);
            }
            $jobs_list_maps[$key]['rules'] = self::formatAssociationRulesItems($association_id, $val['id'], $val);
        }
        return $jobs_list_maps;
    }

    /**
     * 计算每日关联奖励点
     *
     * 该方法用于计算参与规则中每天关联的所有奖励点总数
     * 它首先将参与规则的JSON字符串转换为数组，然后计算所有奖励点的总和
     *
     * @param int $association_id 用户ID
     * @param int $user_id 用户ID
     * @param array $jobs 参与规则的JSON字符串，包含每天的奖励点信息
     * @return array 如果转换失败，返回0；否则返回所有奖励点的总和
     */
    public static function formatAssociationRulesItems($association_id, $user_id, $jobs)
    {
        if (!$participation_rules = XypHelper::formatJsonStringToArray($jobs['participation_rules'])) {
            return [];
        }
        $participation_rules_map = [];
        foreach ($participation_rules as $k => $v) {
            $talk_name = '';
            $total_limit = 0;
            $complete_limit = 0;
            if ($v['talk_id']) {
                $talk_name = Huati::where('id', $v['talk_id'])->value('title');
            }
            $image = $video = $text = [
                'stated' => false, 'limit' => 0, 'num' => 0, 'text' => 0, 'talk_id' => 0,
                'talk_name' => '', 'complete_num' => 0, 'complete_stated' => false
            ];
            if (((int)$v['image_stated']) == 1) {
                $image['stated'] = true;
                $image['limit'] = (int)$v['image_limit'];
                $image['num'] = (int)$v['image_num'];
                $image['text'] = (int)$v['image_length'];
                if ($image['talk_id'] = ((int)$v['image_talk_id'])) {
                    $image['talk_name'] = Huati::where('id', $image['image_talk_id'])->value('title');
                }
                $__key = isset($v['__key']) ? $v['__key'] : '';
                $image['complete_num'] = AssociationProgress::getUsersAssociationProgressTotal($user_id, $association_id, $jobs['id'], $__key, 'image');
                $image['complete_stated'] = ($image['limit'] == $image['complete_num']);
                $complete_limit = $image['complete_num'];
                $total_limit = $image['limit'];
            }
            if (((int)$v['video_stated']) == 1) {
                $video['stated'] = true;
                $video['limit'] = (int)$v['video_limit'];
                $video['num'] = (int)$v['video_num'];
                $video['text'] = (int)$v['video_length'];
                if ($video['talk_id'] = ((int)$v['video_talk_id'])) {
                    $video['talk_name'] = Huati::where('id', $video['video_talk_id'])->value('title');
                }
                $__key = isset($v['__key']) ? $v['__key'] : '';
                $video['complete_num'] = AssociationProgress::getUsersAssociationProgressTotal($user_id, $association_id, $jobs['id'], $__key, 'video');
                $video['complete_stated'] = ($video['limit'] == $video['complete_num']);
                $complete_limit += $video['complete_num'];
                $total_limit += $video['limit'];
            }
            if (((int)$v['text_stated']) == 1) {
                $text['stated'] = true;
                $text['limit'] = (int)$v['text_limit'];
                $text['text'] = (int)$v['text_length'];
                if ($text['talk_id'] = ((int)$v['text_talk_id'])) {
                    $text['talk_name'] = Huati::where('id', $text['text_talk_id'])->value('title');
                }
                $__key = isset($v['__key']) ? $v['__key'] : '';
                $text['complete_num'] = AssociationProgress::getUsersAssociationProgressTotal($user_id, $association_id, $jobs['id'], 'text');
                $text['complete_stated'] = ($text['limit'] == $text['complete_num']);
                $complete_limit += $text['complete_num'];
                $total_limit += $text['limit'];
            }
            $participation_rules_map[$k]['total_limit'] = $total_limit;
            $participation_rules_map[$k]['talk_id'] = intval($v['talk_id']);
            $participation_rules_map[$k]['talk_name'] = $talk_name;
            $participation_rules_map[$k]['reward_point'] = (int)(isset($v['reward_point'])?$v['reward_point']:0);
            $participation_rules_map[$k]['complete_limit'] = $complete_limit;
            $participation_rules_map[$k]['complete_stated'] = ($complete_limit == $total_limit);
            $participation_rules_map[$k]['image'] = $image;
            $participation_rules_map[$k]['video'] = $video;
            $participation_rules_map[$k]['text'] = $text;
            $participation_rules_map[$k]['name'] = $v['name'];
        }
        return $participation_rules_map;
    }

    /**
     * 计算每日关联奖励点
     *
     * 该方法用于计算参与规则中每天关联的所有奖励点总数
     * 它首先将参与规则的JSON字符串转换为数组，然后计算所有奖励点的总和
     *
     * @param string $participation_rules 参与规则的JSON字符串，包含每天的奖励点信息
     * @return int 如果转换失败，返回0；否则返回所有奖励点的总和
     */
    public static function getEveryDayAssociationRewardPoint($participation_rules)
    {
        // 将参与规则的JSON字符串转换为数组，以便处理
        if (!$participation_rules = XypHelper::formatJsonStringToArray($participation_rules)) {
            // 如果转换失败，返回0
            return 0;
        }

        // 计算并返回所有奖励点的总和
        return array_sum(array_column($participation_rules, 'reward_point'));
    }

}